<template></template>

<script>
import { getUser } from '../../api/modules/user'
import { getSchool } from '../../api/modules/school'
import { getStudent } from '../../api/modules/student'
import { getTeacher } from '../../api/modules/teacher'
import { getClass } from '../../api/modules/class'
import { getglbConfig } from '../../api/modules/user'
import { getSchoolRole } from '../../api/modules/school';
import { groupBy } from '../../utils/index'

import { init } from '../../utils/getData';

const app = getApp()

export default {
  mounted () {
    // this.init()
    init({jumpHome: true}); // 获取全局的数据
  },

  methods: {
    async init () {
      const token = uni.getStorageSync('token')
      if (!token) {
        console.log(1111)
        uni.navigateTo({
          url: '/pages/home/home'
        })
        return
      }

      const user = await getUser()
      app.globalData.userInfo = user.data

      const {
        data: { list: schools }
      } = await getSchool()
      const {
        data: { list: classs }
      } = await getClass()
      const {
        data: { list: rolesList }
      } = await getSchoolRole();
      const {
        data: { list: students }
      } = await getStudent({page_no: 1, page_size: 10000}) // 一次性拿 1000条数据
      const {
        data: { list: teachers }
      } = await getTeacher()
      const {
        data
      } = await getglbConfig() // 获取全局配置文件

      const tempSchools = (schools || []).map((school)=>({
        ...school,
        classs: classs.filter((class_)=>class_.sid === school.id),
        students: students.filter((student)=>student.sid === school.id),
        teachers: teachers.filter((teacher)=>teacher.sid === school.id),
        // roles: rolesList.filter((roles)=>roles.sid === school.id)
      }))


      app.globalData.glb_list.students = students; // 获取全局学生的数据
      app.globalData.glb_list.teachers = teachers; // 获取全局老师的数据
      app.globalData.glb_list.classList = classs; // 获取全局班级的数据
      app.globalData.glb_list.rolesList = rolesList; // 获取用户角色的数据
      app.globalData.glb_list.config = data; // 获取全局的配置文件


      app.globalData.schools = tempSchools
      app.globalData.school = tempSchools[0]
      uni.navigateTo({
        url: '/pages/home/home'
      })
    }
  }
}
</script>
